//
//  BCNewOrderStuffCollectionViewCell.m
//  BangcaiSale
//
//  Created by Liang Zhang on 16/7/9.
//  Copyright © 2016年 Bangcai. All rights reserved.
//

#import "BCNewOrderStuffCollectionViewCell.h"

@interface BCNewOrderStuffCollectionViewCell () <UITextFieldDelegate, MMNumberKeyboardDelegate>

@end

@implementation BCNewOrderStuffCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    _priceTextFeild.delegate = self;
    // Create and configure the keyboard.
    MMNumberKeyboard *keyboard = [[MMNumberKeyboard alloc] initWithFrame:CGRectZero];
    keyboard.returnKeyTitle = @"确认";
    NSDictionary *dic = [keyboard valueForKey:@"buttonDictionary"];
    [dic[@(12)] setValue:BangcaiBlue forKey:@"fillColor"];
    keyboard.allowsDecimalPoint = YES;
    keyboard.delegate = self;
    _priceTextFeild.inputView = keyboard;
    UIView *accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40.f)];
    accessoryView.backgroundColor = [UIColor whiteColor];
    UILabel *keyboardTipLable = [[UILabel alloc] initWithFrame:accessoryView.bounds];
    keyboardTipLable.font = [UIFont systemFontOfSize:13];
    keyboardTipLable.textColor = BangcaiBlue;
    keyboardTipLable.text = @"请输入预定单价";
    keyboardTipLable.textAlignment = NSTextAlignmentCenter;
    [accessoryView addSubview:keyboardTipLable];
    _priceTextFeild.inputAccessoryView = accessoryView;
}

+ (instancetype)cellWithCollectionView:(UICollectionView *)collectionView {
    static NSString *ID = @"NewOrderStuffCellIdentifer";
    UINib *nib = [UINib nibWithNibName:@"BCNewOrderStuffCollectionViewCell" bundle: [NSBundle mainBundle]];
    [collectionView registerNib:nib forCellWithReuseIdentifier:ID];
    BCNewOrderStuffCollectionViewCell *cell = [[BCNewOrderStuffCollectionViewCell alloc] init];
    return cell;
}

- (IBAction)addAction:(UIButton *)sender {
    NSString *priceString = [self.priceTextFeild.text substringFromIndex:1];
//    if (self.countLable.text.floatValue < 100) {
        int decimalNum = 2; //保留的小数位数
        double price = priceString.doubleValue + 0.01f;
        NSNumberFormatter *nFormat = [[NSNumberFormatter alloc] init];
        [nFormat setNumberStyle:NSNumberFormatterDecimalStyle];
        [nFormat setMaximumFractionDigits:decimalNum];
        NSString *unitPrice = [nFormat stringFromNumber:@(price)];
        self.priceTextFeild.text = [NSString stringWithFormat:@"￥%@",unitPrice];
//    }
}

- (IBAction)decAction:(UIButton *)sender {
    NSString *priceString = [self.priceTextFeild.text substringFromIndex:1];
    if (priceString.floatValue > 0.00f) {
        int decimalNum = 2; //保留的小数位数
        double price = priceString.doubleValue - 0.01f;
        NSNumberFormatter *nFormat = [[NSNumberFormatter alloc] init];
        [nFormat setNumberStyle:NSNumberFormatterDecimalStyle];
        [nFormat setMaximumFractionDigits:decimalNum];
        NSString *unitPrice = [nFormat stringFromNumber:@(price)];
        self.priceTextFeild.text = [NSString stringWithFormat:@"￥%@",unitPrice];    }
}

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    _addButton.userInteractionEnabled = NO;
    _decButton.userInteractionEnabled = NO;
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
    _addButton.userInteractionEnabled = YES;
    _decButton.userInteractionEnabled = YES;
}

-(BOOL)numberKeyboardShouldReturn:(MMNumberKeyboard *)numberKeyboard {
    if (![_priceTextFeild.text hasPrefix:@"￥"]) {
        _priceTextFeild.text = [NSString stringWithFormat:@"￥%@",_priceTextFeild.text];
    }
    
    return YES;
}

@end
